package org.jeecg.modules.wlhy.hyzhencarrier.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.enums.ExamineStatusEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.hycarrier.service.IHyZhenCarrierOrderDriverService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyzhencarrier.entity.HyZhenCarrier;
import org.jeecg.modules.wlhy.hyzhencarrier.service.IHyZhenCarrierService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: hy_zhen_carrier
 * @Author: jeecg-boot
 * @Date: 2023-06-19
 * @Version: V1.0
 */
@Api(tags = "hy_zhen_carrier")
@RestController
@RequestMapping("/hyzhencarrier/hyZhenCarrier")
@Slf4j
public class HyZhenCarrierController extends JeecgController<HyZhenCarrier, IHyZhenCarrierService> {
    @Autowired
    private IHyZhenCarrierService hyZhenCarrierService;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private IHyDriverService hyDriverService;

    @Autowired
    private IHyZhenCarrierOrderDriverService hyZhenCarrierOrderDriverService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired
    private ISysUserService userService;

    /**
     * 分页列表查询
     *
     * @param hyZhenCarrier
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "hy_zhen_carrier-分页列表查询", notes = "hy_zhen_carrier-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HyZhenCarrier hyZhenCarrier,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<HyZhenCarrier> queryWrapper = QueryGenerator.initQueryWrapper(hyZhenCarrier, req.getParameterMap());
        Page<HyZhenCarrier> page = new Page<HyZhenCarrier>(pageNo, pageSize);
        IPage<HyZhenCarrier> pageList = hyZhenCarrierService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hyZhenCarrier
     * @return
     */
    @AutoLog(value = "hy_zhen_carrier-添加")
    @ApiOperation(value = "hy_zhen_carrier-添加", notes = "hy_zhen_carrier-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HyZhenCarrier hyZhenCarrier) {
        HyCustomer customer = userUtil.getCustomer();
        if (customer != null) {
            hyZhenCarrier.setCustomerId(customer.getId());
        }

        String telephone = hyZhenCarrier.getTel();
        if(StringUtils.isEmpty(telephone)){
            return Result.error("手机号不能为空");
        }

        SysUser sysUser = userService.getUserByPhone(telephone);
        int tel = hyZhenCarrierService.count(new QueryWrapper<HyZhenCarrier>()
                .eq("tel", telephone)
        );
        if (sysUser != null) {
            return Result.error("该承运商手机号已存在用户,不能重复注册");
        }
        if(tel != 0){
            return Result.error("该承运商手机号已存在,不能重复注册");
        }
        return hyZhenCarrierService.saveZhenCarrier(hyZhenCarrier);
    }

    /**
     * 编辑
     *
     * @param hyZhenCarrier
     * @return
     */
    @AutoLog(value = "hy_zhen_carrier-编辑")
    @ApiOperation(value = "hy_zhen_carrier-编辑", notes = "hy_zhen_carrier-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HyZhenCarrier hyZhenCarrier) {
        hyZhenCarrierService.updateById(hyZhenCarrier);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "hy_zhen_carrier-通过id删除")
    @ApiOperation(value = "hy_zhen_carrier-通过id删除", notes = "hy_zhen_carrier-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hyZhenCarrierService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "hy_zhen_carrier-批量删除")
    @ApiOperation(value = "hy_zhen_carrier-批量删除", notes = "hy_zhen_carrier-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hyZhenCarrierService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "hy_zhen_carrier-通过id查询", notes = "hy_zhen_carrier-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HyZhenCarrier hyZhenCarrier = hyZhenCarrierService.getById(id);
        if (hyZhenCarrier == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hyZhenCarrier);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hyZhenCarrier
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyZhenCarrier hyZhenCarrier) {
        return super.exportXls(request, hyZhenCarrier, HyZhenCarrier.class, "hy_zhen_carrier");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyZhenCarrier.class);
    }

    @AutoLog(value = "APP车辆信息-司机车辆绑定")
    @ApiOperation(value="APP车辆信息-司机车辆绑定",httpMethod = "POST", notes="APP车辆信息-司机车辆绑定")
    @PutMapping(value = "/binding")
    public Result<?> binding(@RequestBody JSONObject jsonObject) {
        String carrierId = jsonObject.getString("carrierId");
        String driverId = jsonObject.getString("driverId");
        if (StringUtils.isEmpty(driverId)) {
            return Result.error("司机信息不能为空");
        }
        if (StringUtils.isEmpty(carrierId)) {
            return Result.error("承运商信息不能为空");
        }
        HyDriver driver =  hyDriverService.getById(driverId);
        if(driver == null){
            return Result.error("您的司机信息不存在！");
        }
        HyZhenCarrier zhenCarrier = hyZhenCarrierService.getById(carrierId);
        if(zhenCarrier == null){
            return Result.error("承运商信息不存在！");
        }
        if(hyZhenCarrierOrderDriverService.binding(driverId,carrierId)){
            return Result.OK("绑定成功！");
        }else{
            return Result.error("该承运商已绑定过该司机！");
        }
    }

    @AutoLog(value = "APP车辆信息-司机车辆绑定")
    @ApiOperation(value="APP车辆信息-司机车辆绑定",httpMethod = "POST", notes="APP车辆信息-司机车辆绑定")
    @PutMapping(value = "/bindingOrder")
    public Result<?> bindingOrder(@RequestBody JSONObject jsonObject) {
        String carrierId = jsonObject.getString("carrierId");
        String orderId = jsonObject.getString("orderId");
        if (StringUtils.isEmpty(orderId)) {
            return Result.error("货源信息不能为空");
        }
        if (StringUtils.isEmpty(carrierId)) {
            return Result.error("承运商信息不能为空");
        }
        HyOrder order =  hyOrderService.getById(orderId);
        if(order == null){
            return Result.error("货源信息信息不存在！");
        }
        HyZhenCarrier zhenCarrier = hyZhenCarrierService.getById(carrierId);
        if(zhenCarrier == null){
            return Result.error("承运商信息不存在！");
        }
        if(hyZhenCarrierOrderDriverService.bindingOrder(orderId,carrierId)){
            return Result.OK("绑定成功！");
        }else{
            return Result.error("该货源已绑定过该承运商！");
        }
    }

}
